<!-- 添加考试 -->
<template>
  <section class="add">
    <el-form ref="form" :model="form" label-width="100px" size="small">
      <el-form-item label="试卷编号" prop="examCode">
        <el-input v-model="form.examCode"></el-input>
      </el-form-item>
      <el-form-item label="试卷名称" prop="source">
        <el-input v-model="form.source"></el-input>
      </el-form-item>

      <el-form-item label="试卷时间" prop="totalTime">
        <el-input v-model="form.totalTime"></el-input>
      </el-form-item>
      <el-form-item label="试卷类型" prop="type">
        <el-input v-model="form.type"></el-input>
      </el-form-item>
      <el-form-item label="试卷介绍" prop="description">
        <el-input v-model="form.description" type="textarea"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">立即创建</el-button>
        <el-button type="text" @click="cancel()">取消</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
  export default {
    data () {
      return {
        form : { //表单数据初始化
          source : null ,
          description : null ,
          institute : null ,
          major : null ,
          grade : null ,
          examDate : null ,
          totalTime : null ,
          totalScore : null ,
          type : null ,
          tips : null ,
          paperId : null ,
          examCode:null
        } ,
        rules : {
          examCode : [
            { required : true , message : "试卷编号不能为空" , trigger : "blur" }
          ] ,
          source : [
            { required : true , message : "试卷名称不能为空" , trigger : "blur" }
          ] ,
          totalTime : [
            { required : true , message : "试卷总时长不能为空" , trigger : "blur" }
          ] ,
          type : [
            { required : true , message : "试卷类型不能为空" , trigger : "blur" }
          ]
        }
      };
    } ,
    methods : {
      formatTime ( date ) { //日期格式化
        let year = date.getFullYear ()
        let month = date.getMonth () + 1 < 10?"0" + ( date.getMonth () + 1 ):date.getMonth () + 1;
        let day = date.getDate () < 10?"0" + date.getDate ():date.getDate ();
        let hours = date.getHours () < 10?"0" + date.getHours ():date.getHours ();
        let minutes = date.getMinutes () < 10?"0" + date.getMinutes ():date.getMinutes ();
        let seconds = date.getSeconds () < 10?"0" + date.getSeconds ():date.getSeconds ();
        // 拼接
        return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
      } ,
      onSubmit () {
        this.$axios ( `/api/examManagePaperId` ).then ( res => {
          this.$axios ( {
            url : '/api/exam' ,
            method : 'post' ,
            data : {
              ... this.form
            }
          } ).then ( res => {
            if ( res.data.code == 200 ) {
              this.$message ( {
                message : '数据添加成功' ,
                type : 'success'
              } )
              this.$router.push ( { path : '/selectExam' } )
            }
          } )
        } )
      } ,
      cancel () { //取消按钮
        this.$refs.form.resetFields ()
      } ,

    }
  };
</script>
<style lang="scss" scoped>
  .add {
    padding: 0px 40px;
    width: 400px;
  }
</style>

